#include "CustomCharacterController.h"

#include "InputsManager.h"





// mario 

void MarioController::update(ccTime dt)
{
	CheckSensor();

	// debug loop in a screen
	/*
	if( m_body->GetPosition().x > 400.0f/PTM_RATIO )
	{
		b2Vec2 pos =  m_body->GetPosition();
		pos.x = 20.0f/PTM_RATIO;
		m_body->SetTransform( pos , 0.0f );
	}
	else if( m_body->GetPosition().x < 20.0f/PTM_RATIO )
	{
		b2Vec2 pos =  m_body->GetPosition();
		pos.x = 400.0f/PTM_RATIO;
		m_body->SetTransform( pos , 0.0f );
	}
	*/


	// touching ground 
	if( m_body->GetLinearVelocity().y == 0.0f )
	{
		if( cmptTouchingGround > 0 )
		{
			cmptTouchingGround --;
		}
		else
		{
			isJumping = false;
		}
	}

	// gravity
	if( IsTouchingGround() == false && IsForcingTouchingWall() == true && m_body->GetLinearVelocity().y < 0.0f )
	{
		addYVelocity( -GravityInWall );

		// max Y speed
		if( abs( m_body->GetLinearVelocity().y ) > MaxSpeedYOnWall ) 
		{
			float sign = m_body->GetLinearVelocity().y >= 0.0f ? 1.0f : -1.0f;
			setYVelocity(MaxSpeedYOnWall * sign);
		}
	}
	else
	{
		addYVelocity( -gravity );

		// max Y speed
		if( abs( m_body->GetLinearVelocity().y ) > maxYVelocity ) 
		{
			float sign = m_body->GetLinearVelocity().y >= 0.0f ? 1.0f : -1.0f;
			setYVelocity(maxYVelocity * sign);
		}
	}

	

	// inputs 

	// Horizontal 

	if ( INPUTS->GetKeyState( KEY_ID::KEY_RIGHT ).Press )
	{
		if( m_body->GetLinearVelocity().x < 0.0f )
		{
			// decc
			addXVelocity( InvertXForce );
		}
		else if( m_body->GetLinearVelocity().x < maxXVelocity )
		{
			// acc
			if( ! IsTouchingGround() )
				addXVelocity( AirAcc );
			else
				addXVelocity( XForce );


			// max speed
			if( m_body->GetLinearVelocity().x > maxXVelocity ) 
				setXVelocity(maxXVelocity);
		}
	}
	else if ( INPUTS->GetKeyState( KEY_ID::KEY_LEFT ).Press )
	{
		if( m_body->GetLinearVelocity().x > 0.0f )
		{
			// dec
			addXVelocity( -InvertXForce );
		}
		else if( m_body->GetLinearVelocity().x > -maxXVelocity )
		{
			// acc
			if( ! IsTouchingGround() )
				addXVelocity( -AirAcc );
			else
				addXVelocity( -XForce );

			// max speed
			if( m_body->GetLinearVelocity().x < -maxXVelocity ) 
				setXVelocity(-maxXVelocity);
		}
	}
	else
	{
		// friction
		if( ! IsTouchingGround() )
		{
			if( m_body->GetLinearVelocity().y > 0 && m_body->GetLinearVelocity().y < MinJumpVelocity )
			{
				if( abs(m_body->GetLinearVelocity().x) >= MinVelocityAirDrag )
				{
					setXVelocity( m_body->GetLinearVelocity().x * CoefAirDrag );
				}
			}
		}
		else
		{
			float sign = m_body->GetLinearVelocity().x >= 0.0f ? 1.0f : -1.0f;
			addXVelocity( (min( abs(m_body->GetLinearVelocity().x) , XFriction ) * sign) * -1 );
		}
	}
	

	// Vertical
	if (  INPUTS->GetKeyState( KEY_ID::KEY_JUMP ).Pressed )
	{
		if( IsTouchingGround() )
		{
			setYVelocity( JumpVelocity );
			isJumping = true;
			cmptTouchingGround = 3;
		}
		else if( IsTouchingWall() )
		{
			setYVelocity( JumpWallVelocityY );
			setXVelocity( sensorWallLeftIsColliding ? JumpWallVelocityX : -JumpWallVelocityX );
			isJumping = true;
			cmptTouchingGround = 3;
		}
	}
	else if (INPUTS->GetKeyState( KEY_ID::KEY_JUMP ).Press == false )
	{
		if( IsTouchingGround() == false && m_body->GetLinearVelocity().y > 0 )
		{
			if(  m_body->GetLinearVelocity().y > MinJumpVelocity )
			{
				setYVelocity( MinJumpVelocity );
			}
		}
	}
}